﻿using System;
using System.Collections.Generic;
using System.Data;
using DBUtility;

namespace BLL.SUBSYSTEM
{
    /// <summary>
    /// 业务逻辑类SUBSYS 的摘要说明。
    /// </summary>
    public class SUBSYS
    {
        private readonly OracleDAL.SUBSYSTEM.SUBSYS dal = new OracleDAL.SUBSYSTEM.SUBSYS();
        private readonly DataSetHelper dsh = new DataSetHelper();

        public SUBSYS()
        { }
        #region  成员方法
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SUBSYSTEM.SUBSYS GetModel(string SUBSYSTEMID, string MODULEID)
        {

            return dal.GetModel(SUBSYSTEMID, MODULEID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.SUBSYSTEM.SUBSYS> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            List<Model.SUBSYSTEM.SUBSYS> modelList = new List<Model.SUBSYSTEM.SUBSYS>();
            modelList = DataTableToList(ds.Tables[0]);
            modelList = GetAlarmModel(modelList);
            return modelList;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.SUBSYSTEM.SUBSYS> DataTableToList(DataTable dt)
        {
            List<Model.SUBSYSTEM.SUBSYS> modelList = new List<Model.SUBSYSTEM.SUBSYS>();
            List<Model.SUBSYSTEM.SUBSYS_MODULE> moduleList;// = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.SUBSYSTEM.SUBSYS_MODULE module;
                Model.SUBSYSTEM.SUBSYS model;//= new Model.SUBSYSTEM.SUBSYS(); ;
                string subsysid = string.Empty;
                DataTable dtModule = dsh.Distinct("subsystem", dt, "SUBSYSTEMID");//过滤所有子系统
                rowsCount = dtModule.Rows.Count;
                DataRow[] dr = null;
                for (int i = 0; i < rowsCount; i++)
                {
                    model = new Model.SUBSYSTEM.SUBSYS();
                    moduleList = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
                    model.SUBSYSTEMID = dtModule.Rows[i]["SUBSYSTEMID"].ToString();
                    model.SUBSYSTEMNAME = dtModule.Rows[i]["SUBSYSTEMNAME"].ToString();

                    dr = dt.Select("SUBSYSTEMID='" + dtModule.Rows[i]["SUBSYSTEMID"].ToString() + "'");//从原数据中获取相应子系统的告警
                    int length = dr.Length;
                    for (int j = 0; j < length; j++)
                    {
                        module = new Model.SUBSYSTEM.SUBSYS_MODULE();
                        module.SUBSYSTEMID = dr[j]["SUBSYSTEMID"].ToString();
                        module.MODULEID = dr[j]["MODULEID"].ToString();
                        module.MODULENAME = dr[j]["MODULENAME"].ToString();
                        module.MODULERUNSTATE = dr[j]["MODULERUNSTATE"].ToString();
                        if (dr[j]["COLLECTIONTIME"].ToString() != "")
                        {
                            module.COLLECTIONTIME = DateTime.Parse(dr[j]["COLLECTIONTIME"].ToString());
                        }
                        module.REMARK = dr[j]["REMARK"].ToString();
                        moduleList.Add(module);                        
                    }
                    model.MODULE = moduleList;
                    modelList.Add(model);
                }
                //for (int n = 0; n < rowsCount; n++)
                //{
                //    module = new Model.SUBSYSTEM.SUBSYS_MODULE();
                //    module.SUBSYSTEMID = dt.Rows[n]["SUBSYSTEMID"].ToString();
                //    module.MODULEID = dt.Rows[n]["MODULEID"].ToString();
                //    module.MODULENAME = dt.Rows[n]["MODULENAME"].ToString();
                //    module.MODULERUNSTATE = dt.Rows[n]["MODULERUNSTATE"].ToString();
                //    if (dt.Rows[n]["COLLECTIONTIME"].ToString() != "")
                //    {
                //        module.COLLECTIONTIME = DateTime.Parse(dt.Rows[n]["COLLECTIONTIME"].ToString());
                //    }
                //    module.REMARK = dt.Rows[n]["REMARK"].ToString();
                //    moduleList.Add(module);
                //    model.MODULE = moduleList;
                //    if (0 == n)
                //    {
                //        subsysid = dt.Rows[n]["SUBSYSTEMID"].ToString();
                //        model.SUBSYSTEMID = subsysid;
                //        model.SUBSYSTEMNAME = dt.Rows[n]["SUBSYSTEMNAME"].ToString();
                //    }
                //    if (dt.Rows[n]["SUBSYSTEMID"].ToString() != subsysid)
                //    {
                //        subsysid = dt.Rows[n]["SUBSYSTEMID"].ToString();
                //        model = new Model.SUBSYSTEM.SUBSYS();
                //        model.SUBSYSTEMID = subsysid;
                //        model.SUBSYSTEMNAME = dt.Rows[n]["SUBSYSTEMNAME"].ToString();
                //        moduleList = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
                //        modelList.Add(model);
                //    }
                //}
                //modelList.Add(model);
            }
            return modelList;
        }
        /// <summary>
        /// 获取子系统告警
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <returns></returns>
        private List<Model.SUBSYSTEM.SUBSYS_ALARM> GetAlarmModel(string SUBSYSTEMID)
        {
            return dal.GetAlarmModel(SUBSYSTEMID);
        }
        /// <summary>
        /// 获取子系统列表
        /// </summary>
        /// <param name="ml">原子系统列表</param>
        /// <returns></returns>
        private List<Model.SUBSYSTEM.SUBSYS> GetAlarmModel(List<Model.SUBSYSTEM.SUBSYS> ml)
        {
            DataSet ds = dal.GetAlarmList("BELONGDOMAIN is not null");//查询所有的告警,部分告警不是子系统，BELONGTRANSYS字段可能为空
            List<Model.SUBSYSTEM.SUBSYS_ALARM> modelList = new List<Model.SUBSYSTEM.SUBSYS_ALARM>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = dsh.Distinct("alarm", ds.Tables[0], "SUBSYSTEMID");//过滤所有子系统
                DataRow [] dr = null;
                int length = dt.Rows.Count;
                for (int i = 0; i < length; i++)
                {
                    if (dt.Rows[i]["SUBSYSTEMID"].ToString().Length > 0) {//将没有配置的系统过滤掉
                        dr = ds.Tables[0].Select("SUBSYSTEMID='" + dt.Rows[i]["SUBSYSTEMID"].ToString() + "'");//从原数据中获取相应子系统的告警
                        modelList = GetModel(dr);//将告警形成MODEL返回
                        ml = CheckModel(ml, modelList, dt.Rows[i]["SUBSYSTEMID"].ToString(), dt.Rows[i]["SUBSYSTEMNAME"].ToString());//如果子系统已经存在，就直接将告警的信息绑定到相应的model，否则新添加一个
                    }
                }
            }
            return ml;
        }
        /// <summary>
        /// 获取告警列表
        /// </summary>
        /// <param name="dr">单个子系统告警数据</param>
        /// <returns></returns>
        private List<Model.SUBSYSTEM.SUBSYS_ALARM> GetModel(DataRow[] dr) {
            List<Model.SUBSYSTEM.SUBSYS_ALARM> modelList = new List<Model.SUBSYSTEM.SUBSYS_ALARM>();
            Model.SUBSYSTEM.SUBSYS_ALARM model = null;
            if (dr != null && dr.Length > 0) {
                int length = dr.Length;
                for (int i = 0; i < length; i++)
                {
                    model = new Model.SUBSYSTEM.SUBSYS_ALARM();
                    model.ALARMLEVEL = dr[i]["ALARMLEVEL"].ToString ();
                    model.ALARMCOUNT = dr[i]["ALARMCOUNT"].ToString ();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 检验初始化Model
        /// </summary>
        /// <param name="ml">系统信息ModelList</param>
        /// <param name="aml">告警ModelList</param>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="SUBSYSTEMNAME">子系统名称</param>
        /// <returns></returns>
        private List<Model.SUBSYSTEM.SUBSYS> CheckModel(List<Model.SUBSYSTEM.SUBSYS> ml,List<Model .SUBSYSTEM .SUBSYS_ALARM > aml, string SUBSYSTEMID, string SUBSYSTEMNAME)
        {
            Model.SUBSYSTEM.SUBSYS m = new Model.SUBSYSTEM.SUBSYS();
            m = CheckModel(ml, SUBSYSTEMID);
            if (m == null)
            {
                m = new Model.SUBSYSTEM.SUBSYS();
                m.SUBSYSTEMID = SUBSYSTEMID;
                m.SUBSYSTEMNAME = SUBSYSTEMNAME;
                ml.Add(m);
            }
            m.ALARM = aml;
            return ml;
        }
        /// <summary>
        /// 获取与子系统ID相等的子系统信息Model
        /// </summary>
        /// <param name="ml"></param>
        /// <param name="SUBSYSTEMID"></param>
        /// <returns></returns>
        private Model.SUBSYSTEM.SUBSYS CheckModel(List<Model.SUBSYSTEM.SUBSYS> ml,string SUBSYSTEMID) {
            if (ml == null) {
                return null;
            }
            foreach (Model.SUBSYSTEM.SUBSYS m in ml)
            {
                if (m.SUBSYSTEMID == SUBSYSTEMID) {
                    return m;
                }
            }
            return null;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
    }
}

